package cash.cmp.personal_financial_system.controller.ws;


import cash.cmp.personal_financial_system.model.SysMsg;
import cash.cmp.personal_financial_system.service.impl.SysMsgServiceImpl;
import cash.cmp.personal_financial_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysMsg")
public class SysMsgController {
    @Autowired
    private SysMsgServiceImpl sysMsgService;

    @RequestMapping("/findMsgById")
    public Result findMsgById(@RequestBody Map<String, String> data  ){
        Integer objectId = Integer.parseInt( data.get("objectId") );
        List<SysMsg> sysMsgs = sysMsgService.findMsgById( objectId );
        return Result.succ(200,"success",sysMsgs);
    }

    //删除用户角色对应
    @RequestMapping("/deleteMsgById")
    public Result deleteMsgById(@RequestBody Map<String, String> data ){
        Integer msgId = Integer.parseInt( data.get("msgId") );
        Boolean flag = sysMsgService.deleteMsgById(msgId);
        if(flag) return Result.succ(200,"success",null);
        return Result.fail(546,"fail",null);
    }

    //插入一条信息
    @RequestMapping("/insertMsg")
    public Result insertMsg(@RequestBody SysMsg sysMsg){
        Boolean flag = sysMsgService.insertMsg(sysMsg);
        if(flag) return Result.succ(200,"success",null);
        return Result.fail(546,"fail",null);
    }

    @RequestMapping("/updateMsg")
    public Result updateMsg(@RequestBody Map<String, String> data){
        Integer type = Integer.parseInt(data.get("type")) ;
        Integer readState = Integer.parseInt(data.get("readState")) ;
        String msgId = data.get("msgId");
        Boolean flag = sysMsgService.updateMsg(msgId,type, readState);
        if(flag) return Result.succ(200,"success",null);
        return Result.fail(546,"fail",null);
    }

}
